 /*
 * @description: 自定义页装修
 * @Author: along
 * @Date: 2020-09-07
 * @Last Modified by: along
 * @Last Modified time: 2020-09-07
 */
<template>
    <div class="container activePage">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-new-box">
                    <p class="header-new-box-title">页面名称:</p>
                    <el-input
                        v-model="formData.keyword"
                        placeholder="请输入页面名称"
                        size="mini"
                        clearable
                        suffix-icon="el-icon-search"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="mini"
                        @click="getTableData(1)"
                    >
                        搜索
                    </el-button>
                </div>
            </div>
            <div class="header-right">
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="mini"
                        @click="newPage()"
                        :style="{fontWeight: 500}"
                    >
                        新建装修页面
                    </el-button>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="activePage"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="title"
                label="自定义页名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            />
            <el-table-column
                prop="update_time"
                label="上次修改时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            >
                <template slot-scope="scope">
                    {{ scope.row.update_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                prop="page_no"
                label="二级页面链接"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            >
                <template slot-scope="scope">
                    <p
                        @click="fnCopy(scope.row.page_no)"
                    >{{ scope.row.page_no | filterNo }}</p>
                </template>
            </el-table-column>
            <el-table-column
                prop="status"
                label="默认状态"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <div class="container-flex">
                        <el-switch
                            v-model="scope.row.status"
                            :active-value="1"
                            :inactive-value="0"
                            @change="fnChangeActive(scope.row)"
                        />
                        <span :style="{color: scope.row.status == 1 ? '#666666' : '#E5E5E5', marginLeft: '10px'}">
                            {{ scope.row.status == 1 ? '正常' : '下架' }}
                        </span>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="left"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        @click="fnEditPage(scope.row)"
                    >
                        装修页面
                    </el-button>
                    <el-button
                        type="text"
                        @click="deletePage(scope.row)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- /分页 -->
        <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>

        <!-- 图片预览 -->
        <img-preview ref="preview" />
    </div>
</template>

<script>
import imgPreview from '@/components/common/img-preview';
import { back } from '@/mixin/back';
export default {
    mixins: [back],
    components: {
        imgPreview
    },
    data () {
        return {
            formData: {
                keyword: ''
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {}
        };
    },
    filters: {
        filterNo (val) {
            if(val) {
                let base = location.host == 'phaadmin.wxhand.com' ? BASE_URL : BASE_URL_DEV;
                let ticket = localStorage.getItem('sessionTicket');
                let url = location.host == 'phaadmin.wxhand.com' ? `http://student.zsdx.cn/pages/active/active?page_no=${val}` : `http://student-dev.wxhand.com/pages/active/active?page_no=${val}`;

                return url;
            }
        }
    },
    computed: {},
    watch: {},
    mounted () {
        this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '商城'},{title: '首页模块'},{title: '装修模版'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();

        console.log('authResult', this.authResult);
    },
    methods: {
        /**
         * @description 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/custom_page%5Cget_list', {
                page_id: page || this.page.now_page,
                title: this.formData.keyword
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * @description 新建装修页面
         */
        newPage () {
            if(!this.authResult.auth_add) {
                this.$notify({
                    title: '提示',
                    message: '此权限仅限超级管理员',
                    type: 'warning',
                    duration: 2000
                });
                return;
            }
            this.$router.push({
                name: 'activeTemplate',
                query: {
                    page_no: ''
                }
            });
        },

        /**
         * @description 装修页面
         */
        fnEditPage (row) {
            if(!this.authResult.auth_edit) {
                this.$notify({
                    title: '提示',
                    message: '此权限仅限超级管理员',
                    type: 'warning',
                    duration: 2000
                });
                return;
            }
            this.$router.push({
                name: 'activeTemplate',
                query: {
                    page_no: row.page_no,
                    active_menu: 1
                }
            });
        },

        /**
         * @description 删除页面
         */
        deletePage (row) {
            if(!this.authResult.auth_delete) {
                this.$notify({
                    title: '提示',
                    message: '此权限仅限超级管理员',
                    type: 'warning',
                    duration: 2000
                });
                return;
            }
            this.$confirm('删除后将不可参与该活动,是否删除', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/custom_page%5Cdelete',{
                    page_no: row.page_no
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            message: '删除成功',
                            type: 'success',
                            duration: 2000
                        });
                    } else if (resp.code == 0) {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            type: 'warning',
                            duration: 2000
                        });
                    }
                });
            });
        },

        /**
         * @description 显示/隐藏
         */
        fnChangeActive (row) {
            if(!this.authResult.auth_status) {
                this.$notify({
                    title: '提示',
                    message: '此权限仅限超级管理员',
                    type: 'warning',
                    duration: 2000
                });
                this.getTableData();
                return;
            }

            this.$post('/student/custom_page%5Cset_status',{
                page_no: row.page_no,
                status: row.status
            },resp=>{
                if (resp.code == 1) {
                    this.getTableData();
                    this.$notify({
                        title: '提示',
                        message: '操作成功',
                        type: 'success',
                        duration: 2000
                    });
                } else if (resp.code == 0) {
                    this.getTableData();
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        type: 'warning',
                        duration: 2000
                    });
                }
            });
        },

        /**
         * @description 复制链接
         */
        fnCopy (content) {
            let base = this.$baseUrl;
            let ticket = localStorage.getItem('sessionTicket');
            let url = `${this.$studentBaseUrl}/pages/active/active?page_no=${content}`;

            if(url) {
                let str = url;
                let oInput = document.createElement("input");

                oInput.value = str;
                document.body.appendChild(oInput);
                oInput.select();
                document.execCommand("Copy");
                oInput.style.display = "none";
                this.$message.success('复制成功');
            } else {
                this.$notify({
                    title: '提示',
                    message: '复制失败',
                    type: 'warning',
                    duration: 2000
                });
            }
        },

        /**
         * @description 预览页面
         */
        fnSetQrLink(row) {
            let base = this.$baseUrl;
            let ticket = localStorage.getItem('sessionTicket');
            let url = `${this.$studentBaseUrl}/pages/acitive/acitive?page_no=${row.page_no}`;
            let link = `${base}/__local/common%5Cqrcode?content=${url}&__ticket=${ticket}`;

            return link;
        },

        /**
         * @description 查看图片
         * @param   {String}    images    单图预览
         * @param   {Array}     images     多图预览
         */
        fnShowPreview(images) {
            if(images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images);
            } else {
                this.$notify({
                    title: '提示',
                    message: '图片类型错误',
                    type: 'warning',
                    duration: 2000
                });
            }
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 0px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        margin-bottom: 0px !important;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            margin: 5px 0 5px 0;
            .header-left, .header-right {
                display: flex;
                align-items: center;
                flex-wrap: wrap;
            }
            .header-new-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                white-space: nowrap;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
    .container-flex {
        display: flex;
        height: 32px;
        align-items: center;
        justify-content: center;
        position: relative;
        left: 10px;
        .zd-dialog-title {
            color: #333333;
            font-size: 14px;
            white-space: nowrap;
            margin-right: 20px;
        }
    }
    .container-foot {
        display: flex;
        height: 32px;
        align-items: center;
        justify-content: center;
        margin-top: 42px;
    }
    .preview-qr-wrap {
        width: 120px;
        height: 120px;
        text-align: center;
        margin: 0 auto;
        img {
        width: 100%;
        height: 100%;
        }
    }
</style>
<style lang="less">
    .activePage{
        .el-table__body-wrapper .el-table__body .el-table__row td {
            border-right: none;
        }
        .el-table__header-wrapper .el-table__header .has-gutter tr th {
            border-right: none;
        }
        .el-table .el-table_1_column_6  .cell {
            position: relative;
        }
        .el-range-editor--small .el-range-separator{
            padding: 0 1px !important;
        }
        .el-dialog__header {
            padding-top: 10px;
            box-shadow:0px 1px 0px 0px rgba(233,233,233,1);
            display: flex;
            align-items: center;
            background: #FAFAFA;
            border-radius: 4px 4px 0 0;
            .el-dialog__title {
                font-weight: 500;
                color: #333;
                font-size: 14px;
            }
            .el-icon-close:before {
                color: rgb(194,194,194);
                font-size: 17px;
                font-weight: bold;
            }
            .el-dialog__headerbtn {
                top: 14px;
            }
        }
        .el-button span {
            font-weight: 500;
        }
        .el-table__row td div {
            color: #333;
        }
        .el-table__row td .cell{
            line-height: 40px;
        }
    }
</style>
